<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="olHead">
                        		<li>
		                        	<a href="javascript:getData(0)">顶级分类列表</a>
		                        </li>
<!--		                        <li>-->
<!--		                       		<a href="#" >珠宝</a>-->
<!--		                        </li>-->
<!--		                        <li>-->
<!--		                        	<a href="#" >银饰</a>-->
<!--		                        </li>-->
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o" ></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemTable">
<!--			                          <tr >-->
<!--			                              <td><input  type="checkbox" ></td>			                              -->
<!--				                          <td>982</td>-->
<!--									      <td>吊坠/项链</td>									    -->
<!--									      <td>-->
<!--									      	11    -->
<!--									      </td>									      -->
<!--		                                  <td class="text-center">		                                     -->
<!--		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
<!--									  <tr >-->
<!--			                              <td><input  type="checkbox"> </td>			                              -->
<!--				                          <td>983</td>-->
<!--									      <td>手镯/手链/脚链</td>									    -->
<!--									      <td>-->
<!--									      	11    -->
<!--									      </td>									      -->
<!--		                                  <td class="text-center">		                                     -->
<!--		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
<!--									  <tr >-->
<!--			                              <td><input  type="checkbox" ></td>			                              -->
<!--				                          <td>984</td>-->
<!--									      <td>戒指/耳饰</td>									    -->
<!--									      <td>-->
<!--									      	11    -->
<!--									      </td>									      -->
<!--		                                  <td class="text-center">		                                     -->
<!--		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<input type="hidden" id="parentId">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id="saveName"></td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" placeholder="商品分类名称" id="name" name="name">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
<!--		      			<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
						<select name="typeId" id="typeId" style="width: 100%">
							<option th:value="-1">请选择</option>
							<option th:each=" temp : ${temp}" th:value="${temp.id}">[[${temp.name}]]</option>
						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <input type="text" name="id" id="id">
					  <tr>
						  <td>上级商品分类</td>
						  <td id="updatName"></td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input  class="form-control" placeholder="商品分类名称" name="name" id="name1">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <select name="typeId" id="typeId1" style="width: 100%">
								  <option th:each=" temp : ${temp}" th:value="${temp.id}">[[${temp.name}]]</option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>
<script>
	$(function(){
		getData(0);
		$("#typeId").select2();
	})

	function getData(id){
		$("#parentId").val(id);
		$.ajax({
			url:"/itemCatController/selectItemCat",
			type:"post",
			dataType:"json",
			data:{parentId:id},
			success:function(result){
				if (result.code==200){
				var table = "";
				var list = result.data;
				for (var i=0;i<list.length;i++){
					table += '<tr><td><input  type="checkbox" value="'+ list[i].id +'"></td><td>' + list[i].id +'</td>';
					table += '<td>' + list[i].name +'</td>';
					table += '<td>'+ list[i].typeId +'</td>';
					table += '<td class="text-center">';
					table += '<button type="button" class="btn bg-olive btn-xs" onclick="getData('+ list[i].id +')">查询下级</button>  ';
					table += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal1" onclick="toUpdate('+list[i].id+')">修改</button></td></tr>';
				}
				$("#itemTable").html(table);
				getHead(id);
				}else {
					alert("错误")
				}

			},
			error:function(result){
				alert("错误")
			}

		})
	}

	function getHead(id){
		$.ajax({
			url:"/itemCatController/getItemCatById/" + id,
			type:"post",
			dataType: "json",
			data:{},
			success:function(result){
				var data = result.data;
				//console.log(data)
				if (data == undefined){
					this.data = "";
					//删除他后面所有的元素
					var lis = $("#olHead").find("li");
					for (var i = 1;i<lis.length;i++){
						$(lis[i]).remove();
					}
				}else {
					var list = $('<li id="'+id+'"><a href="javascript:getData('+ id +')">'+data.name+'</a></li>');
					//追加
					list.appendTo($("#olHead"));
					//删除他后面所有的元素
					$("#"+id+"").nextAll().remove();
					console.log("#"+id+"")
				}

				var li = $("#olHead").find("li").find("a");
				var text = "";
				$(li).each(function(i,e){
					if (i==li.length-1){
						text += e.text;
					}else {
						text += e.text + ">>";
					}
				})
				$("#saveName").html(text);
				$("#updatName").html(text);

			},
			error:function(result){
				alert("错误");
			}
		})
	}

	function insert() {
		//获取父级id
		var parentId=$("#parentId").val();
		// alert(parentId)
		var name = $("#name").val();
		//获取select2的数据
		var types = $("#typeId").select2('data');
		var typeId=types.id;
		console.log(typeId)
		$.ajax({
			url:"/itemCatController/insertItemCat",
			type:"post",
			dataType:"json",
			data:{name:name,parentId:parentId,typeId:typeId},
			success:function (result) {
				alert("增加成功");
				location.reload();
			},
			error:function () {
				alert("增加失败");
			}
		})
	}

	function toUpdate(id){
		alert(id)
		$.ajax({
			url:"/itemCatController/getItemCat/"+id,
			type:"post",
			dataType:"json",
			data:{},
			success:function(result){
				if (result.code== 200){
					var data = result.data;
					//console.log(data)
					$("#id").val(data.id);
					$("#name1").val(data.name);
					$("#typeId1").val(data.typeId).trigger("change");
				}
			},
			error:function(result){
				alert("错误")
			}
		})
	}

	function updateInfo(){
		var id = $("#id").val();
		var name = $("#name1").val();
		var typeIds = $("#typeId1").select2();
		var typeId = typeIds.id;
		$.ajax({
			url:"/itemCatController/updateInfo",
			type:"post",
			dataType:"json",
			data:{id:id,name:name,typeId:typeId},
			success:function (result){
				if (result.code == 200){
					alert("修改成功");
					location.reload();
				}
			},
			error:function(result){
				alert("错误");
			}
		})
	}

	function deleteBatch(){
		var boxs = $("[type=checkbox]:checked");
		var ids = [];
		$(boxs).each(function (i,e){
			ids.push(e.value);
		})
		if (ids.length <= 0){
			alert("请选择要删除的行");
			return ;
		}
		console.log(ids);
		if (confirm("确认删除么?")){
			$.ajax({
				url:"/itemCatController/deleteBatch",
				type:"post",
				dataType:"json",
				data:{ids:ids},
				success:function(result){
						alert("删除成功");
						location.reload();
				},
				error:function(result){
					alert("错误");
				}
			})
		}

	}
</script>